/* Includes ------------------------------------------------------------------*/
#include "my_timer.h"
#include "main.h"

/* Exported variables --------------------------------------------------------*/
extern QueueHandle_t	Dclist_Queue;
/* Public variables ---------------------------------------------------------*/
uint8_t  	ms100_flag; 	//产生100ms事件标志
uint8_t  	sec1_flag; 		//产生1s事件标志
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
void Timer0_PWM_Init(void)
{
	// 3. 配置TIMER0
	timer_oc_parameter_struct timer_ocinitpara;
	timer_parameter_struct timer_initpara;
	
	// 1. 使能GPIO和TIMER0时钟
	rcu_periph_clock_enable(RCU_GPIOA);  // 使能GPIOA时钟
	rcu_periph_clock_enable(RCU_TIMER0); // 使能TIMER0时钟
	rcu_periph_clock_enable(RCU_AF);		//使能映射功能的时钟	
	
	// 2. 配置PA8为复用功能（TIMER0_CH0）
	gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);

	// 复位TIMER0配置
	timer_deinit(TIMER0);
	timer_initpara.prescaler         = 120 - 1;             // 预分频器：108MHz / 108 = 1MHz
	timer_initpara.period            = 1000 - 1;            // 自动重装载值：1MHz / 1000 = 1kHz
	timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;  // 边沿对齐模式
	timer_initpara.counterdirection  = TIMER_COUNTER_UP;    // 向上计数
	timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;    // 时钟分频
	timer_initpara.repetitioncounter = 0;                   // 重复计数器
	timer_init(TIMER0, &timer_initpara);

	// 4. 配置TIMER0通道0为PWM模式
	timer_ocinitpara.outputstate  = TIMER_CCX_ENABLE;       // 使能输出
	timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;     // 禁用互补输出
	timer_ocinitpara.ocpolarity   = TIMER_OC_POLARITY_HIGH; // 输出极性：高电平有效
	timer_ocinitpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
	timer_ocinitpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
	timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
	timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocinitpara);
	// 配置PWM模式1
	timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
	timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_ENABLE);
	// 设置占空比（50%）
	timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, 300); // 500 / 1000 = 50%
	// 使能TIMER0
	timer_auto_reload_shadow_enable(TIMER0);
	timer_enable(TIMER0);
	// PWM输出使能
	timer_primary_output_config(TIMER0, ENABLE);
}
/*****************************************************
**\brief      定时器3 100ms初始化
*****************************************************/
void Timer3_10ms_Init(void) 
{ 
	// 使能定时器时钟 
	rcu_periph_clock_enable(RCU_TIMER3); 

	// 定时器参数结构体 
	timer_parameter_struct timer_initpara; 

	// 定时器基本参数配置 
	timer_initpara.prescaler          = 12000-1;   // 预分频器，定时器时钟 = 系统时钟 / (prescaler + 1) 
	timer_initpara.alignedmode        = TIMER_COUNTER_EDGE; 
	timer_initpara.counterdirection   = TIMER_COUNTER_UP; 
	timer_initpara.period             = 99;    // 自动重载值，定时器溢出时间 = (period + 1) * (时钟周期) 
	timer_initpara.clockdivision      = TIMER_CKDIV_DIV1; 
	timer_initpara.repetitioncounter  = 0; 
	timer_init(TIMER3, &timer_initpara); 

	// 使能定时器更新中断 
	timer_interrupt_enable(TIMER3, TIMER_INT_UP); 
	// 清除定时器更新中断标志 
	timer_interrupt_flag_clear(TIMER3, TIMER_INT_UP); 
	// 使能定时器 
	timer_enable(TIMER3); 
} 

/*****************************************************
**\brief      定时器3中断服务函数
*****************************************************/
void TIMER3_IRQHandler(void) 
{ 
	static uint8_t ms100_cnt=0, sec1_cnt=0;
	
	if(RESET!= timer_interrupt_flag_get(TIMER3, TIMER_INT_UP)){ 
		timer_interrupt_flag_clear(TIMER3, TIMER_INT_UP); 
		    
		/* 产生100ms事件标志计时 */
    ms100_cnt++; //
    if(ms100_cnt >= 10){
      ms100_cnt = 0;
      ms100_flag = 1;
		}
		
		/* 产生1s事件标志计时 */
    sec1_cnt++;
    if(sec1_cnt >= 100){
      sec1_cnt = 0;
      sec1_flag = 1;
			
		}
	} 
} 

